#!/bin/sh

revision="showimage v0.1.7 (2023-06-26 by Mib-Wiki)"
# use --help for more info

export PATH=:/proc/boot:/sbin:/bin:/usr/bin:/usr/sbin:/net/mmx/bin:/net/mmx/usr/bin:/net/mmx/usr/sbin:/net/mmx/sbin:/net/mmx/mnt/app/armle/bin:/net/mmx/mnt/app/armle/sbin:/net/mmx/mnt/app/armle/usr/bin:/net/mmx/mnt/app/armle/usr/sbin
export LD_LIBRARY_PATH=/net/mmx/mnt/app/root/lib-target:/net/mmx/mnt/eso/lib:/net/mmx/eso/lib:/net/mmx/mnt/app/usr/lib:/net/mmx/mnt/app/armle/lib:/net/mmx/mnt/app/armle/lib/dll:/net/mmx/mnt/app/armle/usr/lib
export IPL_CONFIG_DIR=/net/mmx/etc/eso/production

thisname="$(basename $0)"
thisdir="$(dirname $0)"

if [ -z $LOG ]; then
	. $thisdir/../config/GLOBALS
	echo -ne "\n$ME-$thisname---->\n" >> $LOG
fi

MIBCAP=3
. $thisdir/../config/MIBCHECK

DMDT="on -f mmx /net/mmx/eso/bin/apps/dmdt"
DMRCClient="on -f mmx /net/mmx/eso/bin/apps/DMRCClient"


case $1 in

-load) {
# <duration in s> <path> <filename>
# supported image formats: /
# jpeg, png, gif (no animations), bmp

if [ ! -z $1 ]; then
	STOP=0
	if [ -z $2 ]; then
		echo -ne "--- DURATION in s missing\n    0 s will display the image on screen until -unload is used\n"
		STOP=1
	fi
	if [ -z $3 ]; then
		echo -ne "--- PATH to the image file missing\n"
		STOP=1
	fi
	if [ -z $4 ]; then
		echo -ne "--- Filename missing\n"
		STOP=1
	fi
	if [ $STOP = 1 ]; then
		echo -ne "test\n"
		exit 0
	fi
fi

	trap '' 2

	if [[ "$TRAINVERSION" = *MHIG* ]]; then
		LOADANDSHOWIMAGE="on -f mmx /net/mmx/eso/bin/apps/loadandshowimage"
		export LIBIMG_CFGFILE=/net/mmx/etc/config/img.conf 2>> $LOG
		$LOADANDSHOWIMAGE ${3}${4}&
		$DMRCClient -mv 99 2>> $LOG # trigger display of image

		$DMRCClient -mv 99 2>> $LOG # trigger display of image
		if [[ $2 = 0 ]]; then
			echo -ne "No end time for image set\nStop image display with -unload\n" | $TEE -i -a $LOG
		else
			sleep $2
			$LOADANDSHOWIMAGE 1 2 3 4 5 #unload image
			$DMRCClient -mv 2>> $LOG # switch back to standard output screen
		fi
	else
		LOADANDSHOWIMAGE="/net/mmx/eso/bin/apps/loadandshowimage"
		export LIBIMG_CFGFILE=/net/mmx/etc/imgprocessing.cfg
		if [[ $5 = "SWDL" ]]; then
			on -f mmx $LOADANDSHOWIMAGE ${3}${4}& >> $LOG
		else
			$LOADANDSHOWIMAGE ${3}${4}& >> $LOG
		fi
		$DMDT sc 0 -9 2>> $LOG # trigger display of image
		if [[ $2 = 0 ]]; then
			echo -ne "No end time for image set\n--- Stop image display with -unload\n" | $TEE -i -a $LOG
		else
			sleep $2
			$LOADANDSHOWIMAGE 1 2 3 4 5 >> $LOG #unload image
			$DMDT sb 0 2>> $LOG # switch back to standard output screen
		fi
	fi
	trap 2

	return 2> /dev/null

};;

-unload) {

	trap '' 2

	echo -ne "--- Stopping image display\n" | $TEE -i -a $LOG

	if [[ "$TRAINVERSION" = *MHIG* ]]; then
		LOADANDSHOWIMAGE="on -f mmx /net/mmx/eso/bin/apps/loadandshowimage"
		export LIBIMG_CFGFILE=/net/mmx/etc/config/img.conf 2>> $LOG
		$LOADANDSHOWIMAGE 1 2 3 4 5 >> $LOG #unload image
		$DMRCClient -mv 2>> $LOG # switch back to standard output screen
	else
		LOADANDSHOWIMAGE="/net/mmx/eso/bin/apps/loadandshowimage"
		export LIBIMG_CFGFILE=/net/mmx/etc/imgprocessing.cfg
		$LOADANDSHOWIMAGE 1 2 3 4 5 >> $LOG #unload image
		$DMDT sb 0 2>> $LOG # switch back to standard output screen
	fi

	trap 2

	return 2> /dev/null

	};;

		-ts) {
	# Take a screenshot from screen 0 and dump to *.png file
	# <time until screenshot> <file name appendix>
	trap '' 2

	if [[ "$TRAINVERSION" = *MHIG* ]]; then
		echo -ne "No support for MHIG...\n" | $TEE -a $LOG
		exit 0
	fi

	if [ -z $2 ]; then
		sleep 0
	else
		sleep $2
	fi

	ID=$(/net/rcc/usr/bin/date +%Y_%m_%d_%H_%M_%S)

	/net/mmx/eso/bin/apps/dmdt ts 0 "/net/mmx/fs/sda0/SCREENSHOT_${3}${ID}.png"

	return 2> /dev/null

};;

	# help or unknown parameter ------------------------------
	*) {
		echo ""
		echo $revision
		echo ""
		echo "Usage: "$thisname" "
		echo ""
		echo "Options:"
		echo "        -load 	[DURATION] [PATH] [FILE]	loads defined image to screen"
		echo "        -unload 				unloads image from screen"
		echo "        -ts 	[time [s] until screenshot]	[file name appendix]	take screenshot of screen"
		echo ""
		echo "This program is free software; you can redistribute it and/or"
		echo "modify it under the terms of the GNU General Public License"
		echo "as published by the Free Software Foundation; either version 2"
		echo "of the License, or (at your option) any later version."
		echo ""
		echo "This program is distributed in the hope that it will be useful,"
		echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
		echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
		echo "See the GNU General Public License for more details."
		echo ""
		echo "You should have received a copy of the GNU General Public License"
		echo "along with this program; if not, write to the Free Software Foundation,"
		echo "Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA."
		echo ""

	};;

	esac

exit 0
